package com.dc.toolkit.hutool.core;

import cn.hutool.core.date.*;
import org.junit.Test;

import java.util.Calendar;
import java.util.Date;

/**
 * Description: Hutool DateUtil Demo
 * @author: gdc
 * @date: 2021/5/8
 * @version 1.0
 */
public class DateUtilTest {
	
	/**
	 * DateUtil.date()  ||  DateUtil.now()
	 * 获取当前日期，格式为：yyyy-MM-dd hh:mm:ss
	 * DateUtil.today()
	 * 获取当前日期，格式为：yyyy-MM-dd
	 */
	@Test
	public void test_date_or_now() {
		DateTime date = DateUtil.date();
		System.out.println("DateUtil.date() = " + date);
		DateTime date2 = DateUtil.date(Calendar.getInstance());
		System.out.println("DateUtil.date() = " + date2);
		DateTime date3 = DateUtil.date(System.currentTimeMillis() - 1000);
		System.out.println("DateUtil.date() = " + date3);
		String now = DateUtil.now();
		System.out.println("DateUtil.now() = " + now);
	}

	/**
	 * DateUtil.today()
	 * 获取当前日期，格式为：yyyy-MM-dd
	 */
	@Test
	public void test_today() {
		String today = DateUtil.today();
		System.out.println("DateUtil.today() = " + today);
	}
	
	/**
	 * 字符串转日期
	 * DateUtil.parse方法会自动识别一些常用格式，包括：
	 * yyyy-MM-dd HH:mm:ss
	 * yyyy-MM-dd
	 * HH:mm:ss
	 * yyyy-MM-dd HH:mm
	 * yyyy-MM-dd HH:mm:ss.SSS
	 */
	@Test
	public void test_parse() {
		DateTime dateTime = DateUtil.parse("2018-10-10");
		System.out.println(dateTime);

		DateTime dateTime2 = DateUtil.parse("2018-10-10 12:30:00");
		System.out.println(dateTime2);

		DateTime dateTime3 = DateUtil.parse("12:30:00");
		System.out.println(dateTime3);
	}
	
	/**
	 * DateUtil.format() 
	 * 格式化日期输出
	 */
	@Test
	public void test_format() {
		String str1 = DateUtil.format(new Date(),"yyyyMMdd");
		System.out.println(str1);
		String date = DateUtil.formatDate(new Date());
		System.out.println(date);
		String time = DateUtil.formatTime(new Date());
		System.out.println(time);
	}
	
	/**
	 * DateUtil.year(Date);		获取Date对象的某个部分
	 * DateUtil.monthEnum(date)	获取月份枚举
	 * DateUtil.dayOfMonth()    获取月份的天
	 */
	@Test
	public void test_get() {
		Date date = new Date();
		int year = DateUtil.year(date);
		System.out.println(year);
		int month = DateUtil.month(date);
		System.out.println(month);
		Month m = DateUtil.monthEnum(date);
		System.out.println(m);
		int day = DateUtil.dayOfMonth(date);
		System.out.println(day);
	}
	
	/**
	 * 获取给定日期的起始、结束日期
	 * DateUtil.beginOfDay(date);
	 * DateUtil.endOfDay(date);
	 * DateUtil.endOfMonth(date);
	 */
	@Test
	public void test_begin_or_end() {
		Date date = new Date();
		DateTime beginDate = DateUtil.beginOfDay(date);
		System.out.println(beginDate);
		DateTime endOfDay = DateUtil.endOfDay(date);
		System.out.println(endOfDay);
		DateTime endOfMonth = DateUtil.endOfMonth(date);
		System.out.println(endOfMonth);
		DateTime beginOfYear = DateUtil.beginOfYear(date);
		System.out.println(beginOfYear);
	}
	
	/**
	 *  日期偏移量
	 *  DateUtil.yesterday()			获取昨天的日期
	 *  DateUtil.tomorrow()				获取明天的日期
	 *  DateUtil.lastWeek()				获取上个星期的日期
	 *  DateUtil.nextWeek()				获取下个星期的日期
	 *  DateUtil.lastMonth()			获取上个月的日期
	 *  DateUtil.nextMonth()			获取下个月的日期
	 */
	@Test
	public void test6() {
		DateTime yesterday = DateUtil.yesterday();
		System.out.println("获取昨天的日期：" + yesterday);
		DateTime tomorrow = DateUtil.tomorrow();
		System.out.println("获取明天的日期：" + tomorrow);
		DateTime lastWeek = DateUtil.lastWeek();
		System.out.println("获取上个星期的日期：" + lastWeek);
		DateTime nextMonth = DateUtil.nextMonth();
		System.out.println("获取下个月份的日期：" + nextMonth);
	}
	
	/**
	 * 日期时间差
	 * DateUtil.between()
	 * 格式化时间差
	 * DateUtil.formatBetween()
	 */
	@Test
	public void test_between() {
		DateTime dt1 = DateUtil.parse("20161001", "yyyyMMdd");
		DateTime dt2 = DateUtil.parse("20181212", "yyyyMMdd");
		long days = DateUtil.between(dt2, dt1, DateUnit.DAY);
		System.out.println("相差天数：" + days);
		long ms = DateUtil.between(dt2, dt1, DateUnit.MS);
		System.out.println(ms);
		
		String fmtDay = DateUtil.formatBetween(dt2, dt1, BetweenFormatter.Level.DAY);
		System.out.println(fmtDay);
	}
	
	/**
	 * 花费毫秒数
	 * timer.interval();
	 * 返回花费毫秒数，并重置开始时间
	 * timer.intervalRestart();
	 */
	@Test
	public void test_timer() {
		TimeInterval timer = DateUtil.timer();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long interval = timer.interval();
		System.out.println(interval);
		timer.intervalRestart();
		long interval2 = timer.interval();
		System.out.println(interval2);
	}
	
	/**
	 * DateUtil.ageOfNow() 获取给定日期到现在的年数
	 * DateUtil.isLeapYear() 判断给定的年份是否为闰年
	 */
	@Test
	public void test_isLeapYear() {
		int ageOfNow = DateUtil.ageOfNow("1993-06-21");
		System.out.println(ageOfNow);
		int year = 2019;
		boolean leapYear = DateUtil.isLeapYear(year);
		if (leapYear) {
			System.out.println(year + "是闰年");
		} else {
			System.out.println(year + "不是闰年");
		}
	}
}
